package com.nintendo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nintendo.config.MinioConfig;
import com.nintendo.entity.pojo.FriendsRequest;
import com.nintendo.entity.pojo.MyFriends;
import com.nintendo.entity.pojo.Users;
import com.nintendo.entity.vo.FriendRequestVo;
import com.nintendo.entity.vo.MyFriendsVo;
import com.nintendo.enums.SearchFriendsStatusEnum;
import com.nintendo.mapper.FriendsRequestMapper;
import com.nintendo.mapper.MyFriendsMapper;
import com.nintendo.mapper.UsersMapperCustomer;
import com.nintendo.service.UsersService;
import com.nintendo.mapper.UsersMapper;
import com.nintendo.utils.FileUtils;
import com.nintendo.utils.MinioUtils;
import com.nintendo.utils.QRCodeUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 86156
 * @description 针对表【users】的数据库操作Service实现
 * @createDate 2025-02-03 15:23:57
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
        implements UsersService {
    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private Sid sid;
    @Autowired
    private QRCodeUtils qrCodeUtils;
    @Autowired
    private FileUtils fileUtils;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private MinioConfig minioConfig;
    @Autowired
    private MyFriendsMapper myFriendsMapper;
    @Autowired
    private UsersMapperCustomer usersMapperCustomer;
    @Autowired
    private FriendsRequestMapper friendsRequestMapper;
    @Override
    public boolean queryUsernameIsExist(String username) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Users result = userMapper.selectOne(queryWrapper);
        return result != null;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Users queryUserForLogin(String username, String password) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        Users result = userMapper.selectOne(queryWrapper);
        return result;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users saveUser(Users user) {
        String userId = sid.nextShort();
        //  为每个用户生成一个唯一的二维码
        String qrCodePath = "E://user"+userId+"qrcode.png";
        // muxin_qrcode:[username]
        qrCodeUtils.createQRCode(qrCodePath, "muxin_qrcode:"+user.getUsername());
        MultipartFile multipartFile = FileUtils.fileToMultipart(qrCodePath);
        String qrCodeUrl = "";
        try {
            qrCodeUrl = minioUtils.upload(multipartFile);
            qrCodeUrl = "http://" + minioConfig.getEndpoint() + ":" + minioConfig.getPort() + "/" + qrCodeUrl;
        } catch (Exception e) {
            e.printStackTrace();
        }
        user.setQrcode(qrCodeUrl);
        user.setId(userId);
        userMapper.insert(user);
        return user;
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateUserInfo(Users user) {
        userMapper.updateById(user);
        return queryUserById(user.getId());
    }
    private Users queryUserById(String userId){
        return userMapper.selectById(userId);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Integer preconditionSearchFriends(String myUserId, String friendUsername) {
        // 前置条件 -1. 搜索的用户如果不存在 返回[无此用户]
        Users users = queryUserInfoByUsername(friendUsername);
        if(users==null){
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        // 前置条件 -2. 搜索的用户如果是你自己 返回[不能添加自己为好友]
        if(users.getId().equals(myUserId)){
            return SearchFriendsStatusEnum.NOT_YOURSELF.status;
        }
        // 前置条件 -3. 搜索的用户已经是你的好友 返回[该用户已经是你的好友]
        QueryWrapper<MyFriends> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("my_user_id", myUserId);
        queryWrapper.eq("my_friend_user_id", users.getId());
        MyFriends myFriendsRel = myFriendsMapper.selectOne(queryWrapper);
        if(myFriendsRel!=null){
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }
        return SearchFriendsStatusEnum.SUCCESS.status;
    }
    @Override
    public Users queryUserInfoByUsername(String username){
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void sendFriendRequest(String myUserId, String friendUsername) {
        // 根据用户名把朋友信息查询出来
        Users friend =  queryUserInfoByUsername(friendUsername);
        // 1. 查询发送好友请求记录表
        QueryWrapper<FriendsRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_user_id", myUserId);
        queryWrapper.eq("accept_user_id", friend.getId());
        FriendsRequest friendsRequest = friendsRequestMapper.selectOne(queryWrapper);
        if(friendsRequest==null){
            // 2. 如果不是你的好友 并且好友记录没有添加 则新增好友请求记录
            String requestId = sid.nextShort();
            FriendsRequest request = new FriendsRequest();
            request.setId(requestId);
            request.setSendUserId(myUserId);
            request.setAcceptUserId(friend.getId());
            request.setRequestDateTime(new Date());
            friendsRequestMapper.insert(request);
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<FriendRequestVo> queryFriendRequestList(String acceptUserId) {
        return usersMapperCustomer.queryFriendRequestList(acceptUserId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteFriendRequest(String sendUserId, String acceptUserId) {
        QueryWrapper<FriendsRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_user_id", sendUserId);
        queryWrapper.eq("accept_user_id", acceptUserId);
        friendsRequestMapper.delete(queryWrapper);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void passFriendRequest(String sendUserId, String acceptUserId) {
        saveFriends(sendUserId, acceptUserId);
        saveFriends(acceptUserId, sendUserId);
        deleteFriendRequest(sendUserId, acceptUserId);
    }



    private void saveFriends(String sendUserId, String acceptUserId){
        MyFriends myFriends = new MyFriends();
        String recordId = sid.nextShort();
        myFriends.setId(recordId);
        myFriends.setMyFriendUserId(acceptUserId);
        myFriends.setMyUserId(sendUserId);
        myFriendsMapper.insert(myFriends);
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<MyFriendsVo> queryMyFriends(String userId) {
        List<MyFriendsVo> myFriendsVos = usersMapperCustomer.queryMyFriends(userId);
        return myFriendsVos;
    }
}




